<template>
  <div>
    <TopFilter :list="topFilterList" :cols="4" labelWidth="80" @filterChange="changeHandle"> </TopFilter>
    <div
      :style="{
        borderTop: '0px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle" :disabled="isAdd">新增</el-button>
      <el-button size="small" type="primary" :disabled="isEdit" @click="editHandle">编辑</el-button>
      <el-button size="small" type="primary" :disabled="isCancel" @click="cancelBtnClick">取消</el-button>
      <AjaxButton size="small" type="primary" :click="saveDialog" :disabled="isSave">保存</AjaxButton>
    </div>
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="基本信息" name="first"><partInfo ref="partInfo" :type="handleType" :rowData="formData" @search="changeHandle"/></el-tab-pane>
      <el-tab-pane label="库存信息" name="second"><stockInfo ref="stockInfo" :SfetchParams="fetchParams1" :rowData1="formData"/></el-tab-pane>
      <el-tab-pane label="预留信息" name="fourth"><partLocked ref="partLocked" :LfetchParams="fetchParams1"/></el-tab-pane>
      <el-tab-pane label="缺料信息" name="fifth"><partShort ref="partShort" :SHfetchParams="fetchParams1"/></el-tab-pane>
      <el-tab-pane label="变更历史" name="sixth"><partWater ref="parrWater" :WAfetchParams="fetchParams1"/></el-tab-pane>
    </el-tabs>
    <div>
      <BaseDialog title="配件选择" :visible.sync="isShowChose" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
        <chosePart ref="chosePart" :FfetchParams="fetchParams" @confirm="confirmChose" @cancel="isShowChose = false" />
      </BaseDialog>
    </div>
  </div>
</template>
<script>
import { queryPartInfos, partExport, queryUnitCode } from '@/api/part/partInfo';
import partInfo from './partInfo';
import chosePart from './chosePart';
import stockInfo from './stockInfo';
import partLocked from './partLocked';
import partShort from './partShort';
import partWater from './partWater';

export default {
  name: 'partPartinfoManagePartinfo',
  components: {
    partInfo,
    chosePart,
    stockInfo,
    partLocked,
    partShort,
    partWater
  },
  data() {
    return {
      topFilterList: this.createTopFilterList(),
      fetchParams: { pageNum: 1, xhrAbort: true },
      handleType: '',
      isEdit: true,
      isSave: true,
      isAdd: false,
      isCancel: true,
      formData: {},
      isShowChose: false, // 配件选择框
      fetchParams1: { xhrAbort: true },
      activeName: 'first'
    };
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partNo',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'partName',
          placeholder: '请输入...'
        }
      ];
    },
    async changeHandle(val) {
      this.fetchParams.xhrAbort = true;
      this.fetchParams = Object.assign(this.fetchParams, {
        xhrAbort: false,
        ...val,
        begin: val.begin ? val.begin.split(' ')[0] : '',
        end: val.end ? val.end.split(' ')[0] : ''
      });
      console.log(this.fetchParams, 'indexchange::');
      if ((!this.fetchParams.partNo || this.fetchParams.partNo.length < 2) && (!this.fetchParams.partName || this.fetchParams.partName.length < 2)) {
        this.$notify({
          title: '失败',
          message: '请至少在配件代码或配件名称中输入一个不少于两位的数据',
          type: 'warning'
        });
        return;
      }
      let res = await queryPartInfos(this.fetchParams);
      console.log(res);
      let partInfos = res.data.rows;
      if (this.fetchParams.partType) {
        this.handleType = 'edit';
        this.formData = partInfos.find(x => x.partType == this.fetchParams.partType);
        console.log('==1::::', this.formData);
        this.fetchParams1 = { partNo: this.formData.partNo, partType: this.formData.partType };
        this.$refs.partInfo.default1();
        this.isEdit = false;
        this.fetchParams.partType = '';
        return;
      }
      if (partInfos.length > 1) {
        console.log('>1');
        console.log('partInfo::', this.$refs.partInfo);
        // this.$refs.chosePart.getParInfos(this.fetchParams);
        this.isShowChose = true;
        console.log('chosePart::', this.$refs);
      } else if (partInfos.length === 1) {
        this.handleType = 'edit';
        this.formData = partInfos[0];
        this.fetchParams1 = { partNo: partInfos[0].partNo, partType: partInfos[0].partType };
        this.$refs.partInfo.default1();
        console.log('==1::::', this.formData);
        this.isEdit = false;
      } else {
        this.$notify({
          title: '失败',
          message: '未搜索到配件',
          type: 'warning'
        });
      }
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.$refs.partInfo.default();
      this.activeName = 'first';
      this.isCancel = false;
      this.isSave = false;
    },
    addHandle() {
      this.formData = { downTag: '10041002' };
      this.fetchParams1 = { xhrAbort: false };
      this.handleType = 'add';
      this.$refs.stockInfo.new();
      this.activeName = 'first';
      this.$refs.partInfo.new();
      this.isEdit = true;
      this.isAdd = true;
      this.isCancel = false;
      this.isSave = false;
    },
    cancelBtnClick() {
      if (this.formData || this.formData.length == 0) {
        this.formData = {};
      }
      this.handleType = 'edit';
      this.$refs.partInfo.default1();
      this.isCancel = true;
      this.isAdd = false;
      this.isSave = true;
    },
    async saveDialog() {
      let resCode = await this.$refs.partInfo.saveDialog();
      console.log(resCode, 'resCode');
      if (resCode == 200) {
        this.handleType = 'edit';
        this.isEdit = false;
        this.$refs.partInfo.default1();
        this.isAdd = false;
        this.isSave = false;
      }
    },
    confirmChose(val) {
      this.handleType = 'edit';
      console.log('val:::', val);
      this.formData = val;
      this.fetchParams1 = { partNo: val.partNo, partType: val.partType };
      this.isShowChose = false;
      this.$refs.partInfo.default1();
      this.isEdit = false;
    },
    handleClick(tab, event) {
      if (this.activeName == 'first') {
        // this.$refs.partInfo.findByPartId(this.formData);
      } else if (this.activeName == 'second') {
      } else if (this.activeName == 'third') {
      } else if (this.activeName == 'fourth') {
      } else if (this.activeName == 'fifth') {
      } else if (this.activeName == 'sixth') {
      }
    }
  }
};
</script>
